package com.yztc.youbadbad.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yztc.youbadbad.R;
import com.yztc.youbadbad.bean.homepage.Other2Bean;
import com.yztc.youbadbad.bean.homepage.Other2GoodBean;
import com.yztc.youbadbad.bean.homepage.OtherAdBean;
import com.yztc.youbadbad.bean.homepage.ProductBean;
import com.yztc.youbadbad.helper.BitmapCircleTransformation;
import com.yztc.youbadbad.helper.RecyclerViewAdapterHelper;
import com.yztc.youbadbad.ui.homepage.interfaces.IOtherView;
import com.yztc.youbadbad.widget.MyGridView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 首页 推荐的适配器 不同item布局
 */

public class OtherHomepageRecyclerAdapter extends RecyclerViewAdapterHelper<Object> {
    private static final int STATE1 = 0, STATE2 = 1, STATE3 = 2, STATE4 = 3;
    private IOtherView iOtherView;


    public OtherHomepageRecyclerAdapter(Context context, List<Object> list
    ,IOtherView iOtherView) {
        super(context, list);
        this.iOtherView = iOtherView;
    }

    @Override
    public int getItemViewType(int position) {
        if (mList.get(position) instanceof OtherAdBean) {
            return STATE1;
        } else if (mList.get(position) instanceof List) {
            return STATE2;
        } else if (mList.get(position) instanceof Other2GoodBean) {
            return STATE3;
        } else if (mList.get(position) instanceof ProductBean){
            return STATE4;
        }
        return STATE1;
    }

    @Override
    public RecyclerView.ViewHolder onCreateMyViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType) {
            case STATE1:
                view = mInflater.inflate(R.layout.item_other_ad, null);
                return new ViewHolder1(view);
            case STATE2:
                view = mInflater.inflate(R.layout.item_other_secondary, null);
                return new ViewHolder2(view);
            case STATE3:
                view = mInflater.inflate(R.layout.item_other_good, null);
                return new ViewHolder3(view);
            case STATE4:
                view = mInflater.inflate(R.layout.item_other_hot, null);
                return new ViewHolder4(view);
        }
        return null;
    }

    @Override
    public void onBindMyViewHolder(RecyclerView.ViewHolder holder, final int position) {

        if (holder instanceof ViewHolder1) {//广告图片
            ViewHolder1 holder1 = (ViewHolder1) holder;
            OtherAdBean otherAdBean = (OtherAdBean) mList.get(position);
            holder1.titleFirTv.setText(otherAdBean.getAd_word());
            holder1.titleFirTv.setText(otherAdBean.getAd_name());
            Glide.with(mContext).load(otherAdBean.getAd_pic()).into(holder1.eventImgIv);
        } else if (holder instanceof ViewHolder2) {//二级分类gridview
            List<Other2Bean> other2Been = (List<Other2Bean>) mList.get(position);
            Other2GridViewAdapter gridViewAdapter = new Other2GridViewAdapter(mContext, other2Been);
            ((ViewHolder2) holder).other2Gv.setAdapter(gridViewAdapter);
        } else if (holder instanceof ViewHolder3){
            Other2GoodBean goodBean = (Other2GoodBean) mList.get(position);
            OtherGoodRecyclerAdapter goodRecyclerAdapter = new OtherGoodRecyclerAdapter(mContext, goodBean.getList());
            ((ViewHolder3) holder).otherGoodRv.setAdapter(goodRecyclerAdapter);
            LinearLayoutManager manager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
            ((ViewHolder3) holder).otherGoodRv.setLayoutManager(manager);
        }else if (holder instanceof ViewHolder4){
            ProductBean productBean = (ProductBean) mList.get(position);
            ViewHolder4 vh = (ViewHolder4) holder;

            vh.brandName.setText(productBean.getBrand_name());
            vh.countryName.setText(productBean.getCountry_name());
            vh.name.setText(productBean.getName());
            vh.storePrice.setText("￥ "+ productBean.getStore_price());

            Glide.with(mContext).load(productBean.getProduct_image()).into(vh.productImage);
            Glide.with(mContext).load(productBean.getCountry_flag())
                    .transform(new BitmapCircleTransformation(mContext))
                    .into(vh.vcountryFlag);

            vh.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    iOtherView.otherProducetItemclicked(position);
                }
            });
        }
    }

    static class ViewHolder1 extends RecyclerView.ViewHolder {
        @BindView(R.id.event_img_iv)
        ImageView eventImgIv;
        @BindView(R.id.title_fir_tv)
        TextView titleFirTv;
        @BindView(R.id.title_sec_tv)
        TextView titleSecTv;


        public ViewHolder1(View itemView) {

            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    static class ViewHolder2 extends RecyclerView.ViewHolder {
        @BindView(R.id.other_2_gv)
        MyGridView other2Gv;


        public ViewHolder2(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    static class ViewHolder3 extends RecyclerView.ViewHolder{
        @BindView(R.id.other_good_rv)
        RecyclerView otherGoodRv;


        public ViewHolder3(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    static class ViewHolder4 extends RecyclerView.ViewHolder{
        @BindView(R.id.product_image)
        ImageView productImage;
        @BindView(R.id.brand_name)
        TextView brandName;
        @BindView(R.id.vcountry_flag)
        ImageView vcountryFlag;
        @BindView(R.id.name)
        TextView name;
        @BindView(R.id.store_price)
        TextView storePrice;
        @BindView(R.id.country_name)
        TextView countryName;


        public ViewHolder4(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
